package com.koron.standard.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 将list转化为树形list
 * @author jzp
 * @since 2021/2/22 9:56
 */
public class TreeUtils {

    private static String s = "[\n" +
            "    {\n" +
            "      \"createTime\": 1612337185000,\n" +
            "      \"name\": \"通用字典\",\n" +
            "      \"updateTime\": 1612427596000,\n" +
            "      \"id\": \"9C736A9B9044426286315982DDD5A583\",\n" +
            "      \"parentId\": \"0\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"createTime\": 1612514542000,\n" +
            "      \"name\": \"巡视管理\",\n" +
            "      \"updateTime\": 1612514542000,\n" +
            "      \"id\": \"C51B3B79923A4436B70EC65047A48A56\",\n" +
            "      \"parentId\": \"0\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"createTime\": 1612428639000,\n" +
            "      \"name\": \"测试2\",\n" +
            "      \"updateTime\": 1612429812000,\n" +
            "      \"id\": \"E3F73688AF974C0CBD049BF412D66952\",\n" +
            "      \"parentId\": \"0\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"createTime\": 1612234909000,\n" +
            "      \"name\": \"项目模板库\",\n" +
            "      \"updateTime\": 1612234909000,\n" +
            "      \"id\": \"F7216FEEA9574EADBAA8E2D0E24577A2\",\n" +
            "      \"parentId\": \"0\"\n" +
            "    }\n" +
            "  ]";

    /**
     * 将list转化为树形list
     * 例如：
     *  [{id:1, parentId:0}, {id:2, parentId:1}]
     *  转化为
     *  [{id:1, parentId:0, children: [ {id:2, parentId:1, children: []}]}]
     *
     * @param list 需要转化成树的list对象
     * @param idFunc 获取id的方法，如 SysDataDictClassify::getId
     * @param parentFunc 获取父类id的方法，如SysDataDictClassify::getParentId
     * @return 转化后的树，元素为jsonArray(Object)
     */
    public static <T> List<Object> rootToTree(List<T> list, Function<T, String> idFunc, Function<T, String> parentFunc) {
        return rootToTree(list, idFunc, parentFunc, null);
    }

    public static <T> List<Object> rootToTreeWithRootId(List<T> list, Function<T, String> idFunc, Function<T, String> parentFunc,String rootId) {
        List<T> root = list.parallelStream().filter(e -> parentFunc.apply(e).equals(rootId)).collect(Collectors.toList());// 拿到所有跟节点（过滤所有非0节点）
        return rowsToTree(list, root, idFunc, parentFunc, null);
    }

    /**
     * 和前端对接，需要将name转化为classifyName
     * @param classifyNameFunc 获取name的函数
     */
    public static <T> List<Object> rootToTree(List<T> list, Function<T, String> idFunc, Function<T, String> parentFunc, Function<T, String> classifyNameFunc) {
        List<T> root = list.parallelStream().filter(e -> parentFunc.apply(e).equals("0")).collect(Collectors.toList());// 拿到所有跟节点（过滤所有非0节点）
        return rowsToTree(list, root, idFunc, parentFunc, classifyNameFunc);
    }

    public static <T> List<Object> rowsToTree(List<T> list,List<T> root, Function<T, String> idFunc, Function<T, String> parentFunc, Function<T, String> classifyNameFunc) {
        JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(root));
        for (int i = 0; i < jsonArray.size(); i++) {
            T t = root.get(i);
            List<T> node = list.parallelStream().filter(e -> parentFunc.apply(e).equals(idFunc.apply(t))).collect(Collectors.toList());// 拿到所有父节点为t的数据
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            jsonObject.put("children", rowsToTree(list, node, idFunc, parentFunc, classifyNameFunc));
            if (classifyNameFunc != null) {
                jsonObject.put("classifyName", classifyNameFunc.apply(t));
            }
            jsonArray.set(i, jsonObject);
        }
        return jsonArray;
    }

}
